//
//  HWLoginViewController.m
//  JWC
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 陈帅. All rights reserved.
//
// 第三方登录管理类
//#import "GBLoginManager.h"

#import "HWLoginViewController.h"

@interface HWLoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@property (weak, nonatomic) IBOutlet UIButton *QQBtn;
@property (weak, nonatomic) IBOutlet UIButton *sinaBtn;
@property (weak, nonatomic) IBOutlet UIButton *weChatBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;
@property (weak, nonatomic) IBOutlet UIView *textView;
@property (weak, nonatomic) IBOutlet UILabel *quickLabel;

@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textLeft;

@end

@implementation HWLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.QQBtn.tag = 100;
    self.sinaBtn.tag = 101;
    self.weChatBtn.tag = 102;
    [self.QQBtn addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.sinaBtn addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.weChatBtn addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view from its nib.
}

/*- (void)buttonclick:(UIButton*)button{
    if(button.tag==101){
        
        //新浪
        [GBLoginManager SinaSSO:self AndGetUserinfoBlock:^(NSDictionary *snsAccount) {
            
           
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"iconURL"] forKey:@"iconURL"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"userName"] forKey:@"userName"];
            NSString *iconURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"iconURL"];
            NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"userName"];
            NSLog(@"\n\n\n userName================%@===========",userName);
            NSLog(@"\n\n\n iconURL================%@===========",iconURL);
            [self landingAction];
        }];
        
        
    }else if(button.tag==102){
        
        //微信
        
        [GBLoginManager WeiChatSSO:self AndGetUserinfoBlock:^(NSDictionary *snsAccount) {
            
            
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"iconURL"] forKey:@"iconURL"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"userName"] forKey:@"userName"];
            NSString *iconURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"iconURL"];
            NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"userName"];
            NSLog(@"\n\n\n userName================%@===========",userName);
            NSLog(@"\n\n\n iconURL================%@===========",iconURL);
            
            [self landingAction];
            
        }];
        
        
        
    }else if (button.tag==100){
        //qq
        
        [GBLoginManager QQSSOWithDelegate:self AndGetUserinfoBlock:^(NSDictionary *snsAccount) {
            
            
            
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"iconURL"] forKey:@"iconURL"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount[@"userName"] forKey:@"userName"];
            NSString *iconURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"iconURL"];
            NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"userName"];
            NSLog(@"\n\n\n userName================%@===========",userName);
            NSLog(@"\n\n\n iconURL================%@===========",iconURL);
            
            [self landingAction];
            
        }];
        
    }
}*/
- (void)landingAction{
    NSString *iconURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"iconURL"];
    NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLanding"];
    [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"user"];
    
    
    //NSLog(@"\n\n\n userName================%@===========",userName);
    //NSLog(@"\n\n\n iconURL================%@===========",iconURL);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    });
    
}

- (IBAction)registerBtn:(UIButton *)sender {
    /*if (self.textLeft.constant == 0) { // 显示注册界面
        self.textLeft.constant = - self.view.width;
        sender.selected = YES;
    } else { // 显示登录界面
        self.textLeft.constant = 0;
        sender.selected = NO;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];*/
    
   }

- (IBAction)close:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
   
}

//需要的动画
-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    [UIView animateWithDuration:1.0 animations:^{
        self.closeBtn.transform = CGAffineTransformMakeRotation(M_PI);
        
    }];
    
  
    
    [UIView animateWithDuration:1.0 delay:0.5 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        //忘记密码动画
        [self setupAnimationWithStartRect:CGRectMake(0, 0, 0, CGRectGetHeight(self.forgetBtn.frame)) endRect:CGRectMake(0, 0, CGRectGetWidth(self.forgetBtn.frame), CGRectGetHeight(self.forgetBtn.frame)) object:self.forgetBtn duration:0.5];
        //注册按钮动画
        [self setupAnimationWithStartRect:CGRectMake(-(CGRectGetWidth(self.registerBtn.frame)), 0, 0, CGRectGetHeight(self.registerBtn.frame)) endRect:CGRectMake(0, 0, CGRectGetWidth(self.registerBtn.frame), CGRectGetHeight(self.registerBtn.frame)) object:self.registerBtn duration:0.5];
        
    }];
    
    //快速登录动画
    [self setupAnimationWithStartRect:CGRectMake(self.quickLabel.width/2, 0, 0, CGRectGetHeight(self.quickLabel.frame)) endRect:CGRectMake(0, 0, CGRectGetWidth(self.quickLabel.frame), CGRectGetHeight(self.quickLabel.frame)) object:self.quickLabel duration:0.5];
    
    
    [UIView animateWithDuration:0.1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.QQBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);
        self.sinaBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);
        self.weChatBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 delay:1.5 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
            self.QQBtn.transform = CGAffineTransformIdentity;
            self.sinaBtn.transform = CGAffineTransformIdentity;
            self.weChatBtn.transform = CGAffineTransformIdentity;
            
        } completion:nil];
    }];
    
}

//设置动画
-(void)setupAnimationWithStartRect:(CGRect)startRect endRect:(CGRect)endRect object:(UIView *)view duration:(NSTimeInterval)duration {
    
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithRect:startRect];
    
    UIBezierPath *endPath = [UIBezierPath bezierPathWithRect:endRect];
    
    CAShapeLayer *quickMask = [[CAShapeLayer alloc]init];
    quickMask.path = endPath.CGPath;
    quickMask.fillColor = [UIColor whiteColor].CGColor;
    view.layer.mask = quickMask;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = duration;
    animation.beginTime = CACurrentMediaTime();
    animation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
    animation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    [quickMask addAnimation:animation forKey:@"path"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return  UIStatusBarStyleLightContent;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
